How to highlight a column in Excel

您所在的位置:网站首页 office2010 vba How to highlight a column in Excel

How to highlight a column in Excel

2022-12-16 21:38| 来源: 网络整理| 查看: 265

How-To How to highlight a column in Excel

Microsoft 365, Microsoft Excel, Microsoft Office, Microsoft Office for Mac, Office 2007, Office 2010, Office 2013, Office 2016, Office 2019, Office 2021 / Office LTSC, Office 365 /

Admin Dec 15, 2022 • 8 min read How to highlight a column in Excel

Microsoft 365, Microsoft Excel, Microsoft Office, Microsoft Office for Mac, Office 2007, Office 2010, Office 2013, Office 2016, Office 2019, Office 2021 / Office LTSC, Office 365 /

Excel tricks to highlight selected row, column, heading and more

17 September 2022

Make it easier to see your current cell in an Excel workbook by dynamically highlighting the selected row, column, cell or headings. Heres obvious and more subtle highlighting options plus the downside of highlighting, real world tips and debugging tricks if youre having trouble.

There are many different variations on this method; two colors, headings only, cell only etc. Well also explain the workings so you can change the highlighting to suit yourself.

Large Excel tables can be hard to navigate and ensure youve selected the right cell. Thats especially important when youre filling in the table gradually and in a random order 聽choosing the right cell is important.

We used this trick for a Trivia Quiz worksheet. Managing the scores with all the noise and confusion of an event can be difficult. This highlighting trick makes entering team scores more reliable.

Any modern Excel for Windows or Mac can do this. The Cell() function is essential and was introduced in Excel 2007 for Windows and Excel 2011 for Mac.

Before we start, a little warning. This trick has several steps and can be frustrating at first. Once you get it working, its great but that first try can drive you a little crazy. Weve included some debugging tricks below.

Dynamic highlighting by selection has two ingredients. Conditional formatting which uses the selected cell location as a condition plus a little VBA to make Excel do some extra work.

The magic ingredient 聽SelectionChange

The main trick is to make Excel recalculate the worksheet whenever you switch to another cell. Normally, Excel only recalculates when theres a change in a cell or data refresh.

To do that, use a little VBA code to do something each time the selection changes. Excel has an in-built event for this called Worksheet_SelectionChange all we have to do is give that event something to do.

This code goes into each worksheet that you want it to work in.Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Application.CutCopyMode = False Then Application.Calculate End If End Sub

The code invokes the SelectionChange event then forces Excel to recalculate the worksheet. We dont want that to happen when were cut/copy/pasting so the IF statement stops that.

This little chunk of code has other uses, as youll see in the Headings of a selected cell option below.

Downside of forcing calculation

Forcing Excel to recalculate the worksheet for every cell movement will slow down the entire workbook. Thats true but probably not noticeable except for really large or complex worksheets. Modern Excel is pretty smart about figuring which cells to re-calc when a manual Calculate is done.

Give the highlighting a try, if it becomes a problem, just remove the VBA code or comment out the Application.Calculate line.

The workbook will have to be saved in a macro-enabled .XLSM format which can be an issue in some organizations.

The Conditional Formatting sauce

Weve talked about Excel Conditional Formatting many times before. Usually its to change the look of a cell based on the value in that cell.

This time well get sneaky. Well give Conditional Formatting a little formula that compares the currently selected cell location (row and column) with the cell to be formatted. If the cell is in the same row or column as the cell youve clicked in, the Conditional Formatting will be done. Its an extension of an Office Watch trick from 2015 applying conditional formatting to other cells.

You can just copy/paste the formula below but if you understand how it works, it opens up a lot more possibilities. The alternatives well look at below are mostly about changing this formula:=OR(CELL("col")=COLUMN(),CELL("row")=ROW())

Its not that scary, lets break it down:CELL("col")=COLUMN()

Compares the column number of selected cell CELL(col) with the column of the cell to be formatted COLUMN(), if theyre the same the result is TRUECELL("row")=ROW()

Compares the row number of selected cell CELL(row) with the row number of the cell to be formatted ROW(), if theyre the same the result is TRUE

Each of those tests returns a TRUE or FALSE, we want the formatting to apply when either case is True so both tests are wrapped in the OR function.

(thats why the VBA code is necessary, to make Excel recalculate the CELL() functions each time the selection changes).

Highlight selected row and column

Now lets put all this together to make the row and column highlighting from the first image in this article. If youre trying this for the first time, try this example first because its the basis for all the later variations. Get this one working and the rest will be a doddle.

Select the entire grid or table then Home | Conditional Formatting | New Rule.

Choose Use a formula to determine which cells to format.

Paste in the formula detailed above:=OR(CELL("col")=COLUMN(),CELL("row")=ROW())

Then click Format to select the look you want. The Fill tab changes the cell background color.

Border is also available to change the edges of the cell, theres an example of that below.

Highlight row & column with different colors

Maybe youd prefer the row and column to have different colors or formatting.

Thats just a variation with two conditional formatting rules, one for rows, the other for columns.

Both rules apply to the same range, grid or table.

Above we explained how the condition formula works, here are the two conditions:=CELL("col")=COLUMN() =CELL("row")=ROW()

As you can see, its the two tests without the OR() test to combine them.

Real World tip

Its likely that you or users of the worksheet will ask for changes to the dynamic highlighting. Maybe a change of color or highlighting just rows etc. To make future changes easier, we suggest always setting two conditional formats (one for rows, one for columns). Even if both use the same formatting as in this example.

If the user/client wants a change, all you have to do is alter the formatting. For example, for row highlighting only, just clear the formatting options for the =Cell(col) 聽line.

Dont delete the conditional formatting rule, you may need it again later!

More subtle, less obtrusive formatting

The above column and row formatting options are commonly demonstrated because they are obvious and showy. In many situations something more subtle is better.

Highlight the selected row or column only

The formatting for row and columns, shown above, is also the way to highlight just a row or column.

Use either the row or column conditional formatting.

(we left the column conditional formatting in case we change our mind.)

The formatting is a two-tone gradient available at Format | Fill | Fill Effects | Two colors

Highlight headings of selected cell plus some extras

You might think the full colored lines are too much, how about highlighting just the row & column headings (Row 1 and Column A).

Change the Applies to 聽to just the first row ($A$1:$I$1) or column ($A$1:$A$13).

Extras, fit the first, border formatting

The above example has few extra tricks, because we cant help ourselves and have little fits of enthusiasm.

On the right side youll see Totals and Rank columns with top/bottom border edge formatting, just to show that alternative. Thats one of the options available on the Format Cells | Border tab.

Instead of color fill, try horizontal and vertical borders to show the selected row/ column.

The conditional format only applies to those two columns.

Extras, fit the second, dynamic summary

The second trick is below the table and deserves an article of its own. Its a summary of the selected student (row) that changes according to the cell youre in.

Its an example of whats possible once Excel is recalculating for each selection change. You can make more responsive and informative worksheets.

Do it with the INDIRECT() function which gets the value of another cell.=INDIRECT(ADDRESS(CELL("row"),1))

Plus our old friend Cell() to get the selected cells row or column position.

Highlight just the selected cell

Even more subtle is highlighting just the selected cell. Excel does that automatically with a border around the selection but you can do more than that with conditional formatting. Here the selected cell is bold with yellow fill.

Do it with a simple variation on the very first formula at the start of this article.=AND(CELL("col")=COLUMN(),CELL("row")=ROW())

Instead of OR() use AND() 聽meaning that both conditions have to be TRUE.

Debugging Tips

Doesnt work for you? Try these suggestions to narrow down the problem.

Is the VBA working?

Make sure the VBA code is working by adding a message box to the function eg: Application.Calculate MsgBox ("You got me")

If the function is working in the workbook then every cell selection will bring up a message.

If the message isnt appearing then you know the function isnt working. Most likely the code is not in the correct worksheet.

Applies to

Check the Applies to conditional formatting and that youre looking at the right part of the workbook.

Show formatting rules for: make sure its This worksheet or This Table.

Applies to: check the correct range is selected.

Fast Fill Sequential lists or rows in ExcelExcel Conditional Formatting, beyond the pre-setsText and formatting tricks for ExcelJustify, Fill, Orientation, Shrink to Fit and other Excel formatting tricks.Join Office for Mere Mortalstoday

Office for Mere Mortals is where thousands pick up useful tips and tricks for Word, Excel, PowerPoint and Outlook. 聽Give it a try. You can unsubscribe at any time. We've never spammed or sold addresses since we started over twenty years ago.Invalid email addressThanks for subscribing!

Video li锚n quan


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3